package com.example.librarysystem.Service;

import com.example.librarysystem.Dao.BooksDao;
import com.example.librarysystem.Dao.CommentDao;
import com.example.librarysystem.entity.Comment;
import com.example.librarysystem.entity.CommentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("commentService")
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private BooksDao booksDao;

    @Override
    public Integer selectCount(){
        return commentDao.selectCount();
    }
    @Override
    public List<Comment> selectByBookName(String bookName){
        return commentDao.selectByBookName(bookName);
    }
    @Override
    public List<Comment> selectByBorrowerName(String borrowerName){
        return commentDao.selectByBorrowerName(borrowerName);
    }
    @Override
    public Integer selectCountByBookName(String bookName){
        return commentDao.selectCountByBookName(bookName);
    }
    @Override
    public List<CommentInfo> selectCommentInfo(int borrowerId, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return commentDao.selectCommentInfo(borrowerId, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<CommentInfo> selectCommentInfoSearch(int borrowerId, String bookName, String author, String publishingHouse, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return commentDao.selectCommentInfoSearch(borrowerId,bookName,author,publishingHouse,startCurrentPage,pageSize);
        }
    }
    @Override
    @Transactional(timeout = 30)
    public int deleteCommentById(int id, int score2delete, String bookName){
        int count = commentDao.selectCountByBookName(bookName) - 1;
        int score = booksDao.selectScoreByName(bookName);
        if(count <= 0){
            score = -1;
        }else {
            score = score * (count + 1) - score2delete / count;
        }
        commentDao.deleteCommentById(id);
        booksDao.updateScoreByName(score,bookName);

        return 0;
    }
    @Override
    public Comment selectCommentById(int id){
        return commentDao.selectCommentById(id);
    }
    @Override
    @Transactional(timeout = 30)
    public int updateCommentById(int id, Comment comment, int score2update){
        int count = commentDao.selectCountByBookName(comment.getBookName());
        int score = booksDao.selectScoreByName(comment.getBookName());
        if(count <= 0){
            score = -1;
        }else {
            score = (score * count - score2update + comment.getScore()) / count;
        }
        commentDao.updateCommentById(id, comment.getContent(), comment.getScore());
        booksDao.updateScoreByName(score,comment.getBookName());

        return 0;
    }
    @Override
    public Comment selectCommentByBookAndBorrower(String bookName, int borrowerId){
        return commentDao.selectCommentByBookAndBorrower(bookName, borrowerId);
    }
    @Override
    public int selectExist(String bookName, int borrowerId){
        return commentDao.selectExist(bookName,borrowerId);
    }
    @Override
    @Transactional(timeout = 30)
    public int insertComment(Comment comment){
        int count = commentDao.selectCountByBookName(comment.getBookName());
        int score = booksDao.selectScoreByName(comment.getBookName());
        if(count <= 0){
            score = comment.getScore();
        }else {
            score = (score * count + comment.getScore()) / (count + 1);
        }
        commentDao.insertComment(comment);
        booksDao.updateScoreByName(score,comment.getBookName());
        return 0;
    }
    @Override
    public int selectCommentInfoCount(int borrowerId){
        return commentDao.selectCommentInfoCount(borrowerId);
    }
    @Override
    public int selectCommentInfoSearchCount(int borrowerId, String bookName, String author, String publishingHouse){
        return commentDao.selectCommentInfoSearchCount(borrowerId, bookName,author,publishingHouse);
    }
    @Override
    public List<Comment> selectAll(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return commentDao.selectAll(startCurrentPage, pageSize);
        }
    }
}
